package charazay.tool.ihm.main;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

import charazay.tool.ihm.overview.OverviewPanel;
import charazay.tool.managers.files.FileManager;
import charazay.tool.misc.Session;

public class LoginDialog extends JDialog {

	private JTextField textField;
	private JPasswordField passwordField;
	private OverviewPanel window;
	private JCheckBox chckbxSave;
	
	public LoginDialog(OverviewPanel lwindow) {
		this.window = lwindow;
		this.setBounds(10, 10, 444, 191);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 4;
		gbc_panel_1.gridy = 0;
		getContentPane().add(panel_1, gbc_panel_1);
		
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 4;
		gbc_panel.gridy = 1;
		getContentPane().add(panel, gbc_panel);
		
		JLabel lblLogin = new JLabel("Login");
		GridBagConstraints gbc_lblLogin = new GridBagConstraints();
		gbc_lblLogin.insets = new Insets(0, 0, 5, 5);
		gbc_lblLogin.gridx = 2;
		gbc_lblLogin.gridy = 2;
		getContentPane().add(lblLogin, gbc_lblLogin);
		
		textField = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 0);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 4;
		gbc_textField.gridy = 2;
		getContentPane().add(textField, gbc_textField);
		
		JLabel lblPassword = new JLabel("Password");
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 2;
		gbc_lblPassword.gridy = 3;
		getContentPane().add(lblPassword, gbc_lblPassword);
		
		passwordField = new JPasswordField();
		GridBagConstraints gbc_passwordField = new GridBagConstraints();
		gbc_passwordField.insets = new Insets(0, 0, 5, 0);
		gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordField.gridx = 4;
		gbc_passwordField.gridy = 3;
		getContentPane().add(passwordField, gbc_passwordField);
		
		chckbxSave = new JCheckBox("save");
		GridBagConstraints gbc_chckbxSave = new GridBagConstraints();
		gbc_chckbxSave.insets = new Insets(0, 0, 5, 5);
		gbc_chckbxSave.gridx = 2;
		gbc_chckbxSave.gridy = 4;
		getContentPane().add(chckbxSave, gbc_chckbxSave);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_btnSubmit = new GridBagConstraints();
		gbc_btnSubmit.gridx = 4;
		gbc_btnSubmit.gridy = 5;
		getContentPane().add(btnSubmit, gbc_btnSubmit);
		this.setVisible(true);
		
		btnSubmit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Session.username = textField.getText();
				Session.password = new String(passwordField.getPassword());
	            window.refreshTablePanel();
	            
	            JButton clickedbutton = (JButton)e.getSource();
	            clickedbutton.getTopLevelAncestor().setVisible(false);
	            
	            if (chckbxSave.isSelected()) {
	        		Properties properties = new Properties();
	        		try {
	        		    properties.load(new FileInputStream(FileManager.CONF_FILE));
	        		} catch (IOException ex) {
	        		}
	        		String language = properties.getProperty("language");
	        		String country = properties.getProperty("country");
	            	
	        		try {
	        			
	        		    //properties.load(new FileInputStream(FileManager.CONF_FILE));
	        			properties.put("username", Session.username);
	        			properties.put("password", Session.password);
	        			properties.put("language", language);
	        			properties.put("country", country);
	        			properties.store(new FileOutputStream(FileManager.CONF_FILE), "Login Password save");
	        		} catch (IOException ex) {
	        		}
	            }
			}
		});
	}
}
